using System;
using System.Collections.Generic;
using System.Text;
using Jbe.TestSuite.Help.Interface;
using System.IO;
using System.Reflection;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace Jbe.TestSuite.Help.Domain
{
    internal class DynamicHelpService : IDynamicHelp
    {
        private Uri helpUri;


        public event EventHandler HelpUriChanged;


        public Uri HelpUri
        {
            get { return helpUri; }
            private set
            {
                if (helpUri != value)
                {
                    helpUri = value;
                    OnHelpUriChanged(EventArgs.Empty);
                }
            }
        }


        public void ShowHelp(Assembly executingAssembly, string helpUrl)
        {
            Uri uri = null;

            if (!Uri.TryCreate(helpUrl, UriKind.Absolute, out uri))
            {
                helpUrl = "file://" + Path.GetDirectoryName(executingAssembly.Location) + "/" + helpUrl;

                if (!Uri.TryCreate(helpUrl, UriKind.Absolute, out uri))
                {
                    throw new ArgumentException("helpUrl is invalid");
                }
            }

            Log("ShowHelp Uri: " + uri.ToString());
            HelpUri = uri;
        }

        protected virtual void OnHelpUriChanged(EventArgs e)
        {
            if (HelpUriChanged != null) { HelpUriChanged(this, e); }
        }

        private static void Log(string message)
        {
            LogEntry entry = new LogEntry();
            entry.Message = message;
            entry.Title = "DynamicHelpService";
            entry.Categories.Add("Help");
            Logger.Write(entry);
        }
    }
}
